<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    :fullscreen="true">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="作者id" prop="createUserId" hidden>
      <el-input v-model="dataForm.createUserId" placeholder="作者id"></el-input>
    </el-form-item>
    <el-form-item label="作者名称" prop="createUserName" hidden>
      <el-input v-model="dataForm.createUserName" placeholder="作者名称"></el-input>
    </el-form-item>
    <el-form-item label="文章题目" prop="articleTitle">
      <el-input v-model="dataForm.articleTitle" placeholder="文章题目"></el-input>
    </el-form-item>
    <el-form-item label="文章类型" prop="articleType">
      <el-select  v-model="dataForm.articleType" placeholder="文章类型" value="">
        <el-option   v-for="item in options"
                     :key="item.value"
                     :label="item.label"
                     :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime" hidden>
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime" hidden>
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="文章内容" prop="articleContent">
      <tinymce v-if="this.isRouterAlive" ref="editor" v-model="dataForm.articleContent" placeholder="文章内容" :height="300"/>
    </el-form-item>
    <el-form-item label="是否删除 0:未删除 1:已删除" prop="isDelete" hidden>
      <el-input v-model="dataForm.isDelete" placeholder="是否删除 0:未删除 1:已删除"></el-input>
    </el-form-item>
    <el-form-item label="县" prop="county" hidden>
      <el-input v-model="dataForm.county" placeholder="县"></el-input>
    </el-form-item>
    <el-form-item label="" prop="def0" hidden>
      <el-input v-model="dataForm.def0" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="def1" hidden>
      <el-input v-model="dataForm.def1" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="def2" hidden>
      <el-input v-model="dataForm.def2" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="def3" hidden>
      <el-input v-model="dataForm.def3" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="def4" hidden>
      <el-input v-model="dataForm.def4" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :loading="isLoading">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import Tinymce from '@/components/Tinymce'

  export default {
    data () {
      return {
        visible: false,
        isRouterAlive: true,
        isLoading: false,
        dataForm: {
          id: 0,
          createUserId: this.$store.state.user.id,
          createUserName: this.$store.state.user.name,
          articleTitle: '',
          createTime: '',
          updateTime: '',
          articleContent: '',
          isDelete: '',
          county: '',
          articleType: '',
          def0: '',
          def1: '',
          def2: '',
          def3: '',
          def4: ''
        },
        options: [{
          value: '0',
          label: '国家领导人讲话'
        }, {
          value: '1',
          label: '部长讲话'
        }, {
          value: '2',
          label: '工作部署'
        }, {
          value: '3',
          label: '农业政策'
        }, {
          value: '4',
          label: '权威发布'
        }, {
          value: '5',
          label: '评论社论'
        }],
        dataRule: {
          articleTitle: [
            { required: true, message: '文章题目不能为空', trigger: 'blur' }
          ],
          articleContent: [
            { required: true, message: '文章具体内容不能为空', trigger: 'blur' }
          ],
          articleType: [
            { required: true, message: '文章类型不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    components: {
      Tinymce
    },
    methods: {
      reload () {
        this.isRouterAlive = false
        this.$nextTick(() => (this.isRouterAlive = true))
      },
      init (id) {
        this.dataForm.id = id || 0
        this.isLoading = false
        this.visible = true
        this.reload()
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/revival/revivalcountry/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.createUserName = data.revivalCountry.createUserName
                this.dataForm.articleTitle = data.revivalCountry.articleTitle
                this.dataForm.createTime = data.revivalCountry.createTime
                this.dataForm.updateTime = data.revivalCountry.updateTime
                this.dataForm.articleContent = data.revivalCountry.articleContent
                this.dataForm.isDelete = data.revivalCountry.isDelete
                this.dataForm.county = data.revivalCountry.county
                this.dataForm.articleType = data.revivalCountry.articleType.toString()
                this.dataForm.def0 = data.revivalCountry.def0
                this.dataForm.def1 = data.revivalCountry.def1
                this.dataForm.def2 = data.revivalCountry.def2
                this.dataForm.def3 = data.revivalCountry.def3
                this.dataForm.def4 = data.revivalCountry.def4
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.isLoading = true
            this.$http({
              url: this.$http.adornUrl(`/revival/revivalcountry/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'createUserName': this.dataForm.createUserName,
                'articleTitle': this.dataForm.articleTitle,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'articleContent': this.dataForm.articleContent,
                'isDelete': this.dataForm.isDelete,
                'county': this.dataForm.county,
                'articleType': this.dataForm.articleType,
                'def0': this.dataForm.def0,
                'def1': this.dataForm.def1,
                'def2': this.dataForm.def2,
                'def3': this.dataForm.def3,
                'def4': this.dataForm.def4
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
